package com.hrcx.mystery.utils.quartz.cron;

/**
 * <h1>cron表达式转换器</h1>
 *
 * @author LinXiPeng
 * @className CronParser
 * @date 2021/12/15 14:51
 * Version 1.0
 */
public class CronParser {
    /**
     * 构建Cron表达式
     *
     * @param cronScheduleModel cron表达式配置对象
     * @return cron表达式
     */
    public static String createCronExpression(CronScheduleModel cronScheduleModel) {
        StringBuilder cronExp = new StringBuilder();

        if (null == cronScheduleModel.getJobType()) {
            //执行周期未配置
            throw new NullPointerException("执行周期未配置");
        }

        if (null != cronScheduleModel.getSecond()
                && null == cronScheduleModel.getMinute()
                && null == cronScheduleModel.getHour()) {
            //每隔几秒
            if (cronScheduleModel.getJobType() == 0) {
                cronExp.append("0/").append(cronScheduleModel.getSecond());
                cronExp.append(" ");
                cronExp.append("* ");
                cronExp.append("* ");
                cronExp.append("* ");
                cronExp.append("* ");
                cronExp.append("?");
                return cronExp.toString();
            }

        }

        if (null != cronScheduleModel.getSecond()
                && null != cronScheduleModel.getMinute()
                && null == cronScheduleModel.getHour()) {
            //每隔几分钟
            if (cronScheduleModel.getJobType() == 4) {
                cronExp.append("* ");
                cronExp.append("0/").append(cronScheduleModel.getMinute());
                cronExp.append(" ");
                cronExp.append("* ");
                cronExp.append("* ");
                cronExp.append("* ");
                cronExp.append("?");
                return cronExp.toString();
            }

        }

        if (null != cronScheduleModel.getSecond()
                && null != cronScheduleModel.getMinute()
                && null != cronScheduleModel.getHour()) {
            //秒
            cronExp.append(cronScheduleModel.getSecond()).append(" ");
            //分
            cronExp.append(cronScheduleModel.getMinute()).append(" ");
            //小时
            cronExp.append(cronScheduleModel.getHour()).append(" ");

            //每天
            if (cronScheduleModel.getJobType() == 1) {
                //日
                cronExp.append("* ");
                //月
                cronExp.append("* ");
                //周
                cronExp.append("?");
            }

            //按每周
            else if (cronScheduleModel.getJobType() == 2) {
                //一个月中第几天
                cronExp.append("? ");
                //月份
                cronExp.append("* ");
                //周
                Integer[] weeks = cronScheduleModel.getDayOfWeeks();
                for (int i = 0; i < weeks.length; i++) {
                    if (i == 0) {
                        cronExp.append(weeks[i]);
                    } else {
                        cronExp.append(",").append(weeks[i]);
                    }
                }

            }

            //按每月
            else if (cronScheduleModel.getJobType() == 3) {
                //一个月中的哪几天
                Integer[] days = cronScheduleModel.getDayOfMonths();
                for (int i = 0; i < days.length; i++) {
                    if (i == 0) {
                        cronExp.append(days[i]);
                    } else {
                        cronExp.append(",").append(days[i]);
                    }
                }
                //月份
                cronExp.append(" * ");
                //周
                cronExp.append("?");
            }

        } else {
            //时或分或秒参数未配置
            throw new NullPointerException("时或分或秒参数未配置");
        }
        return cronExp.toString();
    }

    /**
     * 方法摘要：生成计划的详细描述
     */
    public static String createDescription(CronScheduleModel cronScheduleModel) {
        StringBuilder description = new StringBuilder();
        //计划执行开始时间
        if (null != cronScheduleModel.getSecond()
                && null != cronScheduleModel.getMinute()
                && null != cronScheduleModel.getHour()) {
            //按每天
            if (cronScheduleModel.getJobType() == 1) {
                description.append("每天");
                description.append(cronScheduleModel.getHour()).append("时");
                description.append(cronScheduleModel.getMinute()).append("分");
                description.append(cronScheduleModel.getSecond()).append("秒");
                description.append("执行");
            }

            //按每周
            else if (cronScheduleModel.getJobType() == 2) {
                if (cronScheduleModel.getDayOfWeeks() != null && cronScheduleModel.getDayOfWeeks().length > 0) {
                    StringBuilder days = new StringBuilder();
                    for (int i : cronScheduleModel.getDayOfWeeks()) {
                        if (i == 1) {
                            i = 7;
                        } else {
                            i -= 1;
                        }
                        days.append("周").append(i);
                    }
                    description.append("每周的").append(days).append(" ");
                }
                if (null != cronScheduleModel.getSecond()
                        && null != cronScheduleModel.getMinute()
                        && null != cronScheduleModel.getHour()) {
                    description.append(",");
                    description.append(cronScheduleModel.getHour()).append("时");
                    description.append(cronScheduleModel.getMinute()).append("分");
                    description.append(cronScheduleModel.getSecond()).append("秒");
                }
                description.append("执行");
            }

            //按每月
            else if (cronScheduleModel.getJobType() == 3) {
                //选择月份
                if (cronScheduleModel.getDayOfMonths() != null && cronScheduleModel.getDayOfMonths().length > 0) {
                    StringBuilder days = new StringBuilder();
                    for (int i : cronScheduleModel.getDayOfMonths()) {
                        days.append(i).append("号");
                    }
                    description.append("每月的").append(days).append(" ");
                }
                description.append(cronScheduleModel.getHour()).append("时");
                description.append(cronScheduleModel.getMinute()).append("分");
                description.append(cronScheduleModel.getSecond()).append("秒");
                description.append("执行");
            }

        }
        return description.toString();
    }
}